// Red Crawla Classic - Ported by Glaber (original SOC) and MIDIMan (fixes and conversion to Lua)

freeslot(
	"MT_GREENCRAWLACLASSIC",
	"S_GRCRW_STND",
	"S_GRCRW_STNDB",
	"S_GRCRW_RUN",
	"S_GRCRW_RUN2",
	"S_GRCRW_RUN3",
	"S_GRCRW_RUN4",
	"S_GRCRW_RUN5",
	"S_GRCRW_RUN6",
	"S_GRCRW_RUN7",
	"S_GRCRW_RUN8",
	"S_GRCRW_RUN9",
	"S_GRCRW_RUN10",
	"S_GRCRW_RUN11",
	"S_GRCRW_RUN12",
	"S_GRCRW_RUN13",
	"S_GRCRW_RUN14",
	"S_GRCRW_RUN15",
	"S_GRCRW_RUN16",
	"S_GRCRW_RUN17",
	"S_GRCRW_HURT",
	"S_GRCRW_HURT2",
	"SPR_GRCW"
)

local function Crawl_Attack(actor, var1, var2)
	if not (actor and actor.valid) then return end
	if not (actor.target and actor.target.valid) then return end
	
	A_FaceTarget(actor)
	
	// Originally, the Halloween Ghost also had a melee attack, but I decided not to include it for gameplay reasons --MIDIMan
	P_SetObjectMomZ(actor, 20*FRACUNIT)
	S_StartSound(actor, sfx_s3k95)
	actor.greengroundpounding = 1
end

local function Crawl_Land(actor)
	for i = 0, 31 do
		local angle = i * ANGLE_22h
		local distance = 40 * FRACUNIT
		local x = actor.x + FixedMul(sin(angle), distance)
		local y = actor.y + FixedMul(cos(angle), distance)
		local z = actor.z

		local explode = P_SpawnMobj(x, y, z, MT_EXPLODE)
		explode.momx = FixedMul(sin(angle), distance)
		explode.momy = FixedMul(cos(angle), distance)
	end
end

addHook("MobjSpawn", function(mo)
	if not (mo and mo.valid) then return end
	if not (mo.target and mo.target.valid) then return end
	
	mo.jumptics = (5/2)*TICRATE + (P_RandomRange(-1, 2))*TICRATE
	
end, MT_GREENCRAWLACLASSIC)

addHook("MobjThinker", function(mo)
	if not (mo and mo.valid) then return end
	if not (mo.target and mo.target.valid) then return end
	if mo.greengroundpounding
		P_SetObjectMomZ(mo, -FRACUNIT/4, true)
		if mo.state == S_GRCRW_HURT
		or mo.state == S_GRCRW_HURT2
			mo.greengroundpounding = 0
		end
		if P_IsObjectOnGround(mo)
			P_RadiusAttack(mo, mo, 96*FRACUNIT)
			S_StartSound(mo, sfx_s3k9b)
			Crawl_Land(mo)
			mo.greengroundpounding = 0
		end
		return
	end
	
	if mo.jumptics == nil
		mo.jumptics = (5/2)*TICRATE
	end
	
	if mo.jumptics > 0
		mo.jumptics = $ - 1
	else
		Crawl_Attack(mo, 0, 0)
		mo.jumptics = ((5/2)+P_RandomRange(0, 7))*TICRATE
	end
end, MT_GREENCRAWLACLASSIC)

mobjinfo[MT_GREENCRAWLACLASSIC] = {
	//$Name Final Demo Green Crawla
	//$Sprite GRCWA1
	//$Category Retro Enemies (Custom)
	doomednum = 4013, // in xmasified: 4013
	spawnstate = S_GRCRW_STND,
	spawnhealth = 2,
	seestate = S_GRCRW_RUN,
	reactiontime = 32,
	painchance = 50,
	deathstate = S_XPLD_FLICKY,
	deathsound = sfx_pop,
	painstate = S_GRCRW_HURT,
	painsound = sfx_dmpain,
	speed = 2,
	radius = 24*FRACUNIT,
	height = 32*FRACUNIT,
	raisestate = MT_RETROJETBULLET,
	flags = MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE
}

states[S_GRCRW_STND] =	{SPR_GRCW,	A,	5,	A_Look,	0,	0,	S_GRCRW_STND}

states[S_GRCRW_RUN] =	{SPR_GRCW,	A,	1,	A_Chase,	0,	0,	S_GRCRW_RUN2}
states[S_GRCRW_RUN2] =	{SPR_GRCW,	B,	1,	A_Chase,	0,	0,	S_GRCRW_RUN3}
states[S_GRCRW_RUN3] =	{SPR_GRCW,	C,	1,	A_Chase,	0,	0,	S_GRCRW_RUN4}
states[S_GRCRW_RUN4] =	{SPR_GRCW,	D,	1,	A_Chase,	0,	0,	S_GRCRW_RUN5}
states[S_GRCRW_RUN5] =	{SPR_GRCW,	E,	1,	A_Chase,	0,	0,	S_GRCRW_RUN6}
states[S_GRCRW_RUN6] =	{SPR_GRCW,	F,	1,	A_Chase,	0,	0,	S_GRCRW_RUN7}
states[S_GRCRW_RUN7] =	{SPR_GRCW,	G,	1,	A_Chase,	0,	0,	S_GRCRW_RUN8}
states[S_GRCRW_RUN8] =	{SPR_GRCW,	H,	1,	A_Chase,	0,	0,	S_GRCRW_RUN9}
states[S_GRCRW_RUN9] =	{SPR_GRCW,	I,	1,	A_Chase,	0,	0,	S_GRCRW_RUN10}
states[S_GRCRW_RUN10] =	{SPR_GRCW,	J,	1,	A_Chase,	0,	0,	S_GRCRW_RUN11}
states[S_GRCRW_RUN11] =	{SPR_GRCW,	K,	1,	A_Chase,	0,	0,	S_GRCRW_RUN12}
states[S_GRCRW_RUN12] =	{SPR_GRCW,	L,	1,	A_Chase,	0,	0,	S_GRCRW_RUN13}
states[S_GRCRW_RUN13] =	{SPR_GRCW,	M,	1,	A_Chase,	0,	0,	S_GRCRW_RUN14}
states[S_GRCRW_RUN14] =	{SPR_GRCW,	N,	1,	A_Chase,	0,	0,	S_GRCRW_RUN15}
states[S_GRCRW_RUN15] =	{SPR_GRCW,	O,	1,	A_Chase,	0,	0,	S_GRCRW_RUN16}
states[S_GRCRW_RUN16] =	{SPR_GRCW,	P,	1,	A_Chase,	0,	0,	S_GRCRW_RUN17}
states[S_GRCRW_RUN17] =	{SPR_GRCW,	Q,	1,	A_Chase,	0,	0,	S_GRCRW_RUN}

states[S_GRCRW_HURT] =	{SPR_GRCW,	R,	34,	A_Pain,	0,	0,	S_GRCRW_HURT2}
states[S_GRCRW_HURT2] =	{SPR_GRCW,	R,	1,	A_FaceTarget,	0,	0,	S_GRCRW_STND}